package com.hyyt.shopjsp.statistics.controller;

import com.hyyt.shopjsp.basic.pojo.BasicArea;
import com.hyyt.shopjsp.basic.vo.BasicAreaVo;
import com.hyyt.shopjsp.basic.service.IBasicAreaService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.export.ExportExcel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/7/19.
 */

@Controller
@RequestMapping("/back/areaOrdersCount")
public class AreaOrdersCountController extends BaseController {

    private Logger logger = Logger.getLogger(CustomerStatisticsController.class);
    private String basePath = "/back/shop/statistics/";

    @Resource
    private IBasicAreaService basicAreaService;

    /**
     * 跳转区域订单统计页面
     * @param
     * @param
     */
    @RequestMapping("gotoAreaOrdersCountPage")
    public String gotoAreaOrdersCountPage(Map<String, Object> param){
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", "0");
        List<BasicArea> regionLocationList = basicAreaService.queryBasicArea(params);//查询省
        param.put("regionLocationList", regionLocationList);
        return basePath + "areaOrdersCountList";
    }

    /**
     * 区域统计列表
     * @param page
     * @param response
     * @param createTimeStart      开始时间
     * @param createTimeEnd        结束时间
     * @param regionLocation       省
     * @param city                 市
     * @param country              县区
     */
    @RequestMapping(value="listAreaOrdersCounts",method= RequestMethod.POST)
    @ResponseBody
    public void listAreaOrdersCounts(Page page,String createTimeStart,String createTimeEnd,String regionLocation,String city,String country,HttpServletResponse response){
        Map<String, Object> params = new HashMap<>();
        //获取前台查询参数
        if(Utils.objectIsNotEmpty(regionLocation)){
            params.put("regionLocation", regionLocation);
        }
        if(Utils.objectIsNotEmpty(city)){
            params.put("city", city);
        }
        if(Utils.objectIsNotEmpty(country)){
            params.put("country", country);
        }
        if(Utils.objectIsNotEmpty(createTimeStart)){
            params.put("createTimeStart", createTimeStart);
        }
        if(Utils.objectIsNotEmpty(createTimeEnd)){
            params.put("createTimeEnd", createTimeEnd);
        }
        int totalRecordCount = basicAreaService.countAreaOrdersCount(params);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<BasicAreaVo> basicAreaList = basicAreaService.queryAreaOrdersCount(params, page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", basicAreaList);// rows键 存放每页记录 list
        ResultMsg.create(params, response);  //把内容发送到页面
    }

    /**
     * 导出区域统计excel
     * @param createTimeStart
     * @param createTimeEnd
     * @param regionLocation
     * @param city
     * @param country
     * @param request
     * @param response
     */
    @RequestMapping("exportAreaOrdersCountsExcel")
    public void exportAreaOrdersCountsExcel(String createTimeStart, String createTimeEnd, String regionLocation, String city, String country, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> params = new HashMap<>();
        //获取前台查询参数
        if(Utils.objectIsNotEmpty(regionLocation)){
            params.put("regionLocation", regionLocation);
        }
        if(Utils.objectIsNotEmpty(city)){
            params.put("city", city);
        }
        if(Utils.objectIsNotEmpty(country)){
            params.put("country", country);
        }
        if(Utils.objectIsNotEmpty(createTimeStart)){
            params.put("createTimeStart", createTimeStart);
        }
        if(Utils.objectIsNotEmpty(createTimeEnd)){
            params.put("createTimeEnd", createTimeEnd);
        }
        List<BasicAreaVo> basicAreaList = basicAreaService.queryAreaOrdersCount(params);
        SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
        List<Map<String,Object>> areaOrdersCount = new ArrayList<>();
        if(basicAreaList!=null&&basicAreaList.size()>0){
            for (BasicAreaVo basicAreaVo : basicAreaList) {
                Map<String,Object> map = new HashMap<>();
                map.put("fullName", basicAreaVo.getFullName());
                map.put("count", basicAreaVo.getCount());
                map.put("sum", basicAreaVo.getSum());
                areaOrdersCount.add(map);
            }
        }
        String fileName ="区域订单统计";
        String columnNames[] = {"区域名称","订单数量（单）","订单总金额（元）"};
        String keys[] = {"fullName","count","sum"};
        try {
            ExportExcel.download(fileName,areaOrdersCount,columnNames,keys,request,response);
        } catch (IOException e) {
            logger.error("导出Excel失败！");
        }


    }

}
